package multidraw;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

import multidraw.model.DrawingCanvasModel;
import multidraw.model.ModelObserver;

/**
 * 
 * @author Hayley Abbas
 * @version 1.0
 * Modified by Michael Anderson.
 */
public class ClientListPanelView extends JPanel implements ModelObserver {

	private DrawingCanvasModel myModel;
	
	private JLabel myName;
	private JList clientList;
	private DefaultListModel listModel;
	
	private JPanel buttonPanel;
	private JButton transferButton;
	private JButton revokeButton;
	
	public ClientListPanelView(DrawingCanvasModel m, String name) {
		myModel = m;
		myModel.attachObserver(this);
		
		setLayout(new BorderLayout());
		
		myName = new JLabel("<html>Logged in as <i>" + name + "</i></html>");
		add(myName, BorderLayout.NORTH);
		
		listModel = new DefaultListModel();
		clientList = new JList(listModel);
		add(clientList, BorderLayout.CENTER);
		
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(2,1,5,5));
		add(buttonPanel, BorderLayout.SOUTH);
		
		transferButton = new JButton("Transfer Control");
		buttonPanel.add(transferButton);
		
		revokeButton = new JButton("Revoke Control");
		buttonPanel.add(revokeButton);
		
		addClientListPanelListener(new ClientListPanelController(myModel, this));
	}
	
	/* Note
	 * This eventually needs to be a client 
	 */
	
	protected void addClientListPanelListener(ClientListPanelController controller) {
		transferButton.addActionListener((ActionListener)controller);
		revokeButton.addActionListener((ActionListener)controller);
	}
	
	@Override
	public void updateContent() {
		// TODO Auto-generated method stub
		repaint();
	}
	
	public void updateUserList(Vector<String> userList) {
		listModel.clear();
		for (int i = 0; i < userList.size(); i++) {
			listModel.add(i, userList.get(i));
		}
	}
	
	public JButton getTransferButton() {
		return transferButton;
	}
	
	public JButton getRevokeButton(){
		return revokeButton;
	}
	
	public String getSelectedUser(){
	    String name = (String) clientList.getSelectedValue();
	    String[] temp = name.split(" ");
	    return temp[0];
	}
	

}
